/**
 * 
 */
package com.nari.basic.webfilter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.nari.basic.model.IUser;
import com.nari.basic.service.IRoleService;
import com.nari.basic.webfilter.model.ResourceAuthorityConstants;
import com.nari.coreframe.webframe.j2eewrapper.RequestParameterWrapper;
import com.nari.platform.model.WebConstants;

/**
 * 加载系统全局配置参数，提供访问控制、权限匹配等功能
 * @author Xiaopeng
 * @date 2014-5-13
 */
public class LoadConfigPropertiesFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub
	}

	@SuppressWarnings("unchecked")
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException { 
		//获取应用上下文
		HttpServletRequest hreq = (HttpServletRequest)request;
		ServletContext servletContext = hreq.getSession().getServletContext();
		//获取Spring IOC容器
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
		/* 读取Spring配置的键值，getMessage方法与Spring视图资源配置有关 */
		String superManager = webApplicationContext.getMessage("sys.superManager", null, Locale.CHINA);
		String sysadmin = webApplicationContext.getMessage("sys.administratorRole", null, Locale.CHINA);
		String sysShareManager = webApplicationContext.getMessage("sys.projManagerRole", null, Locale.CHINA);
		String sysSalesman = webApplicationContext.getMessage("sys.salespersonRole", null, Locale.CHINA);
		String cardDistributor = webApplicationContext.getMessage("sys.cardDistributorRole", null, Locale.CHINA);
		String superSale_siteOperation = webApplicationContext.getMessage("sys.superSale.siteOperationRole", null, Locale.CHINA);
		//封装系统全局配置参数
		Map<String,String[]> paramWrapper = new HashMap<String,String[]>();
		paramWrapper.putAll(request.getParameterMap());
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_superManager, new String[]{superManager});
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_sysadmin, new String[]{sysadmin});
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_sysShareManager, new String[]{sysShareManager});
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_sysSalesman, new String[]{sysSalesman});
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_cardDistributor, new String[]{cardDistributor});
		paramWrapper.put(ResourceAuthorityConstants.sysconfig_superSale_siteOperation, new String[]{superSale_siteOperation});
		//用户角色权限查询
		IUser loginUser = (IUser) hreq.getSession().getAttribute(WebConstants.USER_SESSION);
		if(loginUser != null) {
			IRoleService iRoleService = (IRoleService) webApplicationContext.getBean("IRoleService");
			try {
				String loginUserRoleIDList = iRoleService.queryRoleIDsByUserID(loginUser.getUserID());
				paramWrapper.put(ResourceAuthorityConstants.loginUser_roleIDList, new String[]{loginUserRoleIDList});
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		request = new RequestParameterWrapper(hreq, paramWrapper);
		chain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
	}

}
